<?php

class RekeningController extends Zend_Controller_Action {

    public function init() {
        /* Initialize action controller here */
        $session = new Zend_Session_Namespace('Zend_Auth');
        $this->sess = $session->data;

        if (!isset($this->sess)) {
            $param = array(
                'action' => 'login',
                'controller' => 'index',
                'msg' => 'Session Timeout silahkan login kembali !'
            );
            $this->_redirect($this->view->url($param, 'default', true));
        }

        $this->_helper->layout->setLayout('rekening');

        $this->request = $this->getRequest();
        $this->param = $this->request->getParams();

        $this->member = new Application_Model_Member();
        $newMembers = $this->member->getNewMembers(20);
        $this->view->newMembers = $newMembers;

        $rewardMember = $this->member->getRewardMembers(20);
        $this->view->rewardMember = $rewardMember;
    }

    public function indexAction() {
        // action body
        $accNum = $this->sess->member_code;
        $modelRek = new Application_Model_Rekening();
        $rekeningList = $modelRek->getAccountList($accNum);

        $modelFunc = new Application_Model_Functions();
        $this->view->today = $modelFunc->gatDayIndoName(date('l')) . ', ' . date('d F Y H:i:s');
        $this->view->rekeningList = $rekeningList;

        $this->render('saldo');
    }

    public function saldoAction() {

        $accNum = $this->sess->member_code;
        $modelRek = new Application_Model_Rekening();
        $rekeningList = $modelRek->getAccountList($accNum);

        $modelFunc = new Application_Model_Functions();
        $this->view->today = $modelFunc->gatDayIndoName(date('l')) . ', ' . date('d F Y H:i:s');
        $this->view->rekeningList = $rekeningList;
    }

    public function mutasiAction() {

        $accNum = $this->sess->member_code;
        $modelRek = new Application_Model_Rekening();
        //$rekeningList = $modelRek->getAccountList($accNum);

        $modelFunc = new Application_Model_Functions();
        $this->view->today = $modelFunc->gatDayIndoName(date('l')) . ', ' . date('d F Y H:i:s');
        //$this->view->rekeningList = $rekeningList;

        $memberId = $this->sess->member_id;

        $dateTmp1 = (isset($this->param['date1'])) ? $this->param['date1'] : date('Y-m-d', mktime(0, 0, 0, date("m"), date("d") - 7, date("Y")));
        $dateTmp2 = (isset($this->param['date2'])) ? $this->param['date2'] : date('Y-m-d');

        $date1 = (isset($this->param['date1'])) ? str_replace('-', '', $this->param['date1']) : date('Ymd', mktime(0, 0, 0, date("m"), date("d") - 7, date("Y")));
        $date2 = (isset($this->param['date2'])) ? str_replace('-', '', $this->param['date2']) : date('Ymd');

        $arrDate = array('date1' => $date1, 'date2' => $date2, 'dateTmp1' => $dateTmp1, 'dateTmp2' => $dateTmp2);

        //$this->member = new Application_Model_Member();
        $data = $modelRek->getTotalSaldo($memberId);

        $this->view->data = array_merge($data, $arrDate);

        $transTypeList = new Application_Model_TransType();
        $this->view->transTypeList = $transTypeList->transTypeList();
    }

    //Inquiry Point
    public function inquiryAction() {

        $memberId = $this->sess->member_id;
        $accNum = $this->sess->member_code;

        if ($this->getRequest()->isPost()) {

            $this->_helper->layout->disableLayout();
            $formData = $this->_request->getPost();


            $accountNum = $formData['account_num'];

            $errorMsg = "";


            if ($errorMsg == "") {

                $modelRekening = new Application_Model_Rekening();
                $data = $modelRekening->inquiryPoint($memberId, $accountNum);

                $code = $data['code'];
                $msg = $data['msg'];

                if ($code == 0) {
                    //Jika disave
                    if ($cbSave != "") {
                        $dataTrans = array(
                            'account_num' => $toOtherAcc,
                            'member_id' => $memberId,
                            'trans_name' => $saveAs,
                            'trans_type_id' => 0
                        );

                        $save = $modelTransfer->saveToPersonalTrans($dataTrans);
                    }

                    $this->view->code = $code;
                    $this->view->msg = $msg;
                } else {
                    $this->view->code = '1';
                    $this->view->msg = "ERROR " . $msg;
                }
            } else {
                $this->view->code = '1';
                $this->view->msg = "ERROR " . $errorMsg;
            }
        } else {
            $modelAcc = new Application_Model_Rekening();
            $accountList = $modelAcc->getAccounts($accNum);

            $modelFunc = new Application_Model_Functions();
            $this->view->today = $modelFunc->gatDayIndoName(date('l')) . ', ' . date('d F Y H:i:s');
            $this->view->accountList = $accountList;
        }
    }

    public function withdrawAction() {
        if ($this->getRequest()->isPost()) {
            $this->_helper->layout->disableLayout();
            
            $formData = $this->_request->getPost();

            //print_r($formData);

            $fromAccount = $formData['acc_num'];
            //$balance = $formData['balance'];
            $tarik = $formData['tarik'];
            $bank = $formData['bank_name'];
            $biaya = 5000;

            
            $errorMsg = "";

            //Pastikan Balance
            $modelRek = new Application_Model_Rekening();
            $getBalance = $modelRek->getTotalBalance($fromAccount);
            $balance = $getBalance["balance"];

            $minSaldo = $formData["min_saldo"];


            //cek minimum saldo
            if ($minSaldo > $balance || $minSaldo > ($balance - ($tarik + $biaya))) {
                $errorMsg .= "* Minimum Saldo tidak mencukupi.<br>";
            }


            /*
             * Cek Penarikan
             */

            if (($tarik + $biaya) > $balance) {
                $errorMsg .= "* Penarikan + Biaya lebih besar daripada jumlah saldo.<br>";
                
            }

            $msg = "";
            if ($errorMsg == "") {

                unset($formData['register']);

                // Insert Withdraw
                $dataW = $modelRek->withdraw($this->sess->member_id, $fromAccount, $tarik, $bank);
                $msg = $dataW["msg"];
                $code = $dataW['code'];

                $this->view->code = $code;

                if ($code == '0') {
                    $this->view->msg = $msg;
                } else {
                    $this->view->msg = "ERROR ".$msg;
                }
            }else{
                $this->view->code = "1";
                $this->view->msg = "ERROR ".$errorMsg;
            }


            //$this->view->id = $msg;
        } else {

            $account = $this->sess->member_code;

            $modelRek = new Application_Model_Rekening();
            $getBalance = $modelRek->getTotalBalance($account);
            $bank = $modelRek->getAccountBank($this->sess->member_id);
            //print_r($getBalance);

            $this->view->acc_num = $account;
            $this->view->balance = $getBalance["balance"];
            $this->view->balance2 = $getBalance["balance2"];
            $this->view->bank = $bank['bank_acc_num'];
        }
    }


    public function topuppointAction() {
        if ($this->getRequest()->isPost()) {
            $this->_helper->layout->disableLayout();

            $formData = $this->_request->getPost();

            //print_r($formData);

            $amount = $formData['amount'];
            //$balance = $formData['balance'];
            $bankAccNo = $formData['bank_account_no'];
            $tglTrans = $formData['tgl_trans'];
            $remark = $formData['remark'];
            
            $errorMsg = "";

            /*
             * Check Captcha Input
             */
            $captcha = new Application_Model_CaptchaValidator();
            if (!$captcha->validateCaptcha($formData['captcha'])){
                $errorMsg .= "* Kode keamanan salah.<br>";                
            }

            $msg = "";
            if ($errorMsg == "") {

                unset($formData['register']);

                // Insert topup
                $modelRek = new Application_Model_Rekening();
                $dataW = $modelRek->topupPoint($this->sess->member_id, $bankAccNo, $amount, $tglTrans, $remark);
                $msg = $dataW["msg"];
                $code = $dataW['code'];

                $this->view->code = $code;

                if ($code == '0') {
                    $this->view->msg = $msg;
                } else {
                    $this->view->msg = "ERROR ".$msg;
                }
            }else{
                $this->view->code = "1";
                $this->view->msg = "ERROR ".$errorMsg;
            }


            //$this->view->id = $msg;
        } else {

            $account = $this->sess->member_code;

            $modelRek = new Application_Model_Rekening();
            $getBalance = $modelRek->getTotalBalance($account);
            $bank = $modelRek->getAccountBankList($this->sess->member_id);

            $this->view->acc_num = $account;
            $this->view->balance = $getBalance["balance"];
            $this->view->balance2 = $getBalance["balance2"];
            $this->view->bank = $bank; //['bank_acc_num'];
        }
    }


    public function bonustrxAction() {

        $dateTmp1 = (isset($this->param['date1'])) ? $this->param['date1'] : date('Y-m-d', mktime(0, 0, 0, date("m"), date("d") - 7, date("Y")));
        $dateTmp2 = (isset($this->param['date2'])) ? $this->param['date2'] : date('Y-m-d');

        $arrDate = array('dateTmp1' => $dateTmp1, 'dateTmp2' => $dateTmp2);

        $this->view->data = $arrDate;

    }

}
